<?php

	require_once(dirname(__FILE__)."/../core/safeParam.php");
	
	class navigationWrapper{
		function prepareURL($text){
			$text = strip_tags($text);
    		$text = $text;
    		$text = str_replace("&ldquo", " ", $text);
    		$text = str_replace("&rdquo", " ", $text);
    		$text = str_replace("&rsquo", " ", $text);
    		$text = str_replace("&lsquo", " ", $text);
    		$text = str_replace("&rdquo", " ", $text);
    		$text = str_replace("&quot", " ", $text);
    		$text = str_replace("&nbsp;", " ", $text);
    		$text = str_replace("&bull", "", $text);
    		$text = str_replace("&#39;", "'", $text);
    		$text = str_replace("?", "", $text);
    		$text = str_replace(" ", "-", $text);
    		$text = str_replace("/", "-", $text);
    		return $text;
  		}
  		
  		function getparentID($id){
    		global $database;
    		$queryfoot = $database->query("SELECT * FROM pages WHERE id = '$id'");
    		$query_num = mysql_num_rows($queryfoot);
    		
    		while ($listfoot = mysql_fetch_array($queryfoot)) {
    			$navid  = $listfoot['id'];
      		$parent = $listfoot['parent'];
    		} //$listfoot = mysql_fetch_array($queryfoot)
    		
    		return $parent;
  		}
  		
  		function dropdownExists($id){
    		global $database;
    		$query    = $database->query("SELECT * FROM pages WHERE parent = '$id'");
    		$num_rows = mysql_num_rows($query);
    		
    		if ($num_rows == '0') {
    			return '0';
    		} //$num_rows == '0'
    		else {
      		return '1';
    		}
  		}
  		
  		function getChildren($id){
    		global $database;
    		$parentid = $id;
    		global $company;
    		$queryfoot = $database->query("SELECT * FROM pages WHERE parent = '$parentid' AND rid = " . RID . " ORDER by pos");
    		
    		while ($listfoot = mysql_fetch_array($queryfoot)) {
    			$sub_navid     = $listfoot['id'];
      		$sub_title     = $listfoot['linktitle'];
      		$sub_name      = $listfoot['pagename'];
      		$sub_name      = $listfoot['pagename'];
      		$sub_status    = $listfoot['status'];
      		$sub_link      = $listfoot['link'];
      		$sub_content   = $listfoot['content'];
      		$menu          = $listfoot['menu'];
      		$sub_clickable = $listfoot['clickable'];
      		$sub_disabled  = $listfoot['disabled'];
      		
      		if ($sub_status == '2') {
      			$navlink = $sub_link;
      		} //$sub_status == '2'
      		else {
      			$navlink = 'sites.php?act=pages&id=' . $sub_navid . '';
      		}
      		
      		if ($menu == "Main Menu") {
      			if ($this->dropdownExists($sub_navid) == "0") {
      				if ($sub_clickable == '0') {
      					$listall .= '<li><a href="#">' . $sub_title . ' </a></li>';
          			} //$sub_clickable == '0'
          			elseif ($sub_disabled == '1') {
          				$listall .= 'a';
          			} //$sub_disabled == '1'
          			else {
          				$sub_listall .= '<li><a href="' . $navlink . '">' . $sub_title . ' </a></li>';
          			}
        			} //$this->dropdownExists($sub_navid) == "0"
        			else {
        				if ($sub_clickable == '0') {
        					$listall .= '<li><a href="#">' . $sub_title . ' </a></li>';
          			} //$sub_clickable == '0'
          			elseif ($sub_disabled == '1') {
          				$listall .= '';
          			} //$sub_disabled == '1'
          			else {
            			$sub_listall .= '<li><a href="/' . $navlink . '">' . $sub_title . ' </a>' . $this->getChildren($sub_navid) . '</li>';
          			}
        			}
      		} //$menu == "Main Menu"
      		
      		$sub_menu = '<ul>	' . $sub_listall . ' </ul>';
    		} //$listfoot = mysql_fetch_array($queryfoot)
    		
    		return $sub_menu;
  		}
  		
  		function getMenuArray($rid = 1){
    		global $database;
    		$results = $database->select("SELECT * FROM pages WHERE parent = '0' AND rid = " . $rid . "  ORDER by pos");
    		
    		if ($results != false) {
    			for ($i = 0; $i < count($results); $i++) {
    				$results[$i]['submenu'] = $database->select("SELECT * FROM pages WHERE parent = '" . $results[$i]['id'] . "' AND rid = " . $rid . "  ORDER by pos");
      		} 
    		} 
    		
    		return $results;
  		}
  		
  		function getMenu(){
  			global $database;
    		
    		$sql = "SELECT * FROM pages WHERE parent = '0' AND rid = " . RID . "  ORDER by pos";
    		
    		$queryfoot = $database->query($sql);
    		$clickable = "";
    		$disabled = "";
    		$ACTIVE = "";
    		$listall = "";
    		
    		while ($listfoot = mysql_fetch_array($queryfoot)) {
    			$navid     = $listfoot['id'];
      		$title     = $listfoot['linktitle'];
      		$name      = $listfoot['pagename'];
      		$name      = $listfoot['pagename'];
      		$status    = $listfoot['status'];
      		$link      = $listfoot['link'];
      		$menu      = $listfoot['menu'];
      		$content   = $listfoot['content'];

      		if (isset($listfoot['clickable'])) {
        			$clickable = $listfoot['clickable'];
      		}
      		
      		if (isset($listfoot['disabled'])) {
        			$disabled  = $listfoot['disabled'];
      		}
      		
      		if (isset($_GET['act']) && $_GET['act'] == 'pages') {
        			if ($_GET['id'] == '') {
          			$activeid = 1;
        			} //$_GET['id'] == ''
        			else {
          			$activeid = $_GET['id'];
        			}
        			
        			if ($activeid == $navid) {
          			$ACTIVE = ' class="active" ';
        			} //$activeid == $navid
        			else {
          			$ACTIVE = '';
        			}
      		} //$_GET['act'] == 'pages'
      		
      		if ($status == '2') {
        			$navlink = $link;
      		} //$status == '2'
      		else {
        			$navlink = 'sites.php?act=pages&id=' . $navid . '';
      		}
      		
      		if ($menu == "Main Menu") {
        			if ($this->dropdownExists($navid) == "0") {
          			if ($clickable == '0') {
            			$listall .= '<li><a href="#" ' . $ACTIVE . '><span class="l"></span><span class="r"></span><span class="t">' . $title . '</span></a></li>';
          			} 
          			elseif ($disabled == '1') {
            			$listall .= '';
          			} 
          			else {
            			$listall .= '<li><a href="' . $navlink . '" ' . $ACTIVE . '><span class="l"></span><span class="r"></span><span class="t">' . $title . '</span></a></li>';
          			}
        			} 
        			else {
          			if ($clickable == '0') {
            			$listall .= '<li><a href="#" ' . $ACTIVE . '></span><span class="r"></span><span class="t">' . $title . '</span></a></li>';
          			} 
          			elseif ($disabled == '1') {
            			$listall .= '';
          			} 
          			else {
            			$listall .= '<li></span><span class="r"></span><span class="t"><a href="' . $navlink . '" ' . $ACTIVE . '>' . $title . ' </a></span>
                                            	' . $this->getChildren($navid) . '</li>';
          			}
        			}
      		} 
    		} 
    		
    		$menu = '<div ><ul  class="art-menu">	' . $listall . ' </ul></div>';
    		return $menu;
  		}
	}
?>
